{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
    "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
    "execution": {
     "iopub.execute_input": "2022-07-31T06:22:21.528294Z",
     "iopub.status.busy": "2022-07-31T06:22:21.527656Z",
     "iopub.status.idle": "2022-07-31T06:22:21.534740Z",
     "shell.execute_reply": "2022-07-31T06:22:21.533143Z",
     "shell.execute_reply.started": "2022-07-31T06:22:21.528249Z"
    },
    "ExecuteTime": {
     "end_time": "2024-07-22T08:30:08.809817300Z",
     "start_time": "2024-07-22T08:30:06.707962700Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.3.1+cpu\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "print(torch.__version__)\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.Tensor的理解：\n",
    "    1.常数，scaler:0阶张量\n",
    "    2.向量,vector,1阶张量\n",
    "    3.矩阵,matrix:2阶张量\n",
    "    4.3阶张量\n",
    "# 2. 创建张量的方法：\n",
    "     1.使用列表创建Tensor\n",
    "     2.使用numpy数组创建Tensor\n",
    "     3.通过torch的API创建Tensor\n",
    "     \n",
    "# 3. 张量的方法和属性\n",
    "    1.tensor.item(),当tensor中只有一个元素可以用的时候\n",
    "    2.Tensor转为ndarray\n",
    "    3.形状修改，tensor.view((3, 4)), 类似numpy中的reshape,是一种浅拷贝\n",
    "    4.获取维数、转置、轴滚动。\n",
    "    5.在方法后加_，会原地修改，相当于Tensorflow里的inplace"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 2. 创建张量的方法："
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T06:27:39.559996Z",
     "iopub.status.busy": "2022-07-31T06:27:39.559579Z",
     "iopub.status.idle": "2022-07-31T06:27:39.576433Z",
     "shell.execute_reply": "2022-07-31T06:27:39.575188Z",
     "shell.execute_reply.started": "2022-07-31T06:27:39.559963Z"
    },
    "ExecuteTime": {
     "end_time": "2024-07-22T08:30:11.793311Z",
     "start_time": "2024-07-22T08:30:11.767007700Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([1., 2., 3.])\n",
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.]])\n"
     ]
    }
   ],
   "source": [
    "# 1.使用列表创建Tensor\n",
    "t1 = torch.Tensor([1, 2, 3])\n",
    "print(t1)\n",
    "\"\"\"\n",
    "tensor([1., 2., 3.])\n",
    "\"\"\"\n",
    "\n",
    "# 2.使用numpy数组创建Tensor\n",
    "array1 = np.arange(12).reshape(3, 4)\n",
    "\n",
    "t2 = torch.Tensor(array1)\n",
    "print(t2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[0., 1., 2., 3.]])"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2[0:1,:]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-22T08:30:37.035401400Z",
     "start_time": "2024-07-22T08:30:37.026345400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0., 0., 0., 0.],\n",
      "        [0., 0., 0., 0.],\n",
      "        [0., 0., 0., 0.]])\n",
      "tensor([[1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.]])\n",
      "tensor([[0., 0., 0., 0.],\n",
      "        [0., 0., 0., 0.],\n",
      "        [0., 0., 0., 0.]])\n",
      "tensor([[0.4446, 0.0484, 0.6733, 0.2621],\n",
      "        [0.6150, 0.9424, 0.7886, 0.2304],\n",
      "        [0.2789, 0.8892, 0.9171, 0.0146]])\n"
     ]
    }
   ],
   "source": [
    "# 3.通过torch的API创建Tensor\n",
    "\"\"\"\n",
    "torch.empty(3,4)：创建3行四列的空的tensor,会用无用的数据进行填充\n",
    "torch.ones([3,4]):三行四列全为1的tensor\n",
    "torch.zeros([3,4]):三行四列全为0的tensor\n",
    "torch.rand([3,4]):三行四列随机值在[0,1]之间的值\n",
    "torch.randint(low = 0, high = 10, size = [3, 4]) 创建3*4的随机整数的Tensor，值区间：[low, high]\n",
    "torch.randn([3,4]) 均值为0，方差为1,3*4的tensor\n",
    "\"\"\"\n",
    "print(torch.empty(3,4))\n",
    "print(torch.ones([3,4]))\n",
    "print(torch.zeros([3,4]))\n",
    "print(torch.rand([3,4]))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T06:37:39.568913Z",
     "start_time": "2024-07-19T06:37:39.549290600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3 张量的方法和属性"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T06:29:04.995479Z",
     "iopub.status.busy": "2022-07-31T06:29:04.995033Z",
     "iopub.status.idle": "2022-07-31T06:29:05.019150Z",
     "shell.execute_reply": "2022-07-31T06:29:05.017794Z",
     "shell.execute_reply.started": "2022-07-31T06:29:04.995441Z"
    },
    "ExecuteTime": {
     "end_time": "2024-04-25T06:44:09.785951300Z",
     "start_time": "2024-04-25T06:44:09.748966Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0], dtype=torch.int32)\n",
      "0\n",
      "--------------------------------------------------\n",
      "1.0\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 1.tensor.item(),当tensor中只有一个元素可以用的时候\n",
    "a =torch.tensor(np.arange(1))\n",
    "print(a)\n",
    "print(a.item()) #只有一个元素的时候可以用,返回一个python的标量,不可以用于多个元素的tensor\n",
    "print('-'*50)\n",
    "print(torch.Tensor([[[1]]]).item()) #获取一个tensor中的元素值\n",
    "print('-'*50)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3. 4.]]\n",
      "torch.Size([1, 2])\n",
      "torch.Size([1, 2])\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 2.Tensor转为ndarray\n",
    "t2 = torch.Tensor([[3,4]])\n",
    "print(t2.numpy()) #tensor转为ndarray\n",
    "\n",
    "print(t2.shape) #获取形状\n",
    "print(t2.size()) #获取形状\n",
    "print(t2.size(1))  #获取某个维度的数据,维度后的张量切片"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T06:39:40.461115400Z",
     "start_time": "2024-07-19T06:39:40.440612400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[3., 4.]])"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T06:40:32.961305Z",
     "start_time": "2024-07-19T06:40:32.953400500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2362134727152\n",
      "2362134725424\n"
     ]
    }
   ],
   "source": [
    "#写一个ndarray\n",
    "array1 = np.array([[1,2,3],[4,5,6]])\n",
    "print(id(array1))\n",
    "array2=array1.reshape(3,2)\n",
    "print(id(array2))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T06:42:23.789723200Z",
     "start_time": "2024-07-19T06:42:23.779767500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[100   2   3]\n",
      " [  4   5   6]]\n",
      "[[100   2]\n",
      " [  3   4]\n",
      " [  5   6]]\n"
     ]
    }
   ],
   "source": [
    "array2[0,0]=100\n",
    "print(array1)\n",
    "print(array2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T06:46:40.677868400Z",
     "start_time": "2024-07-19T06:46:40.670049600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "2"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "array1.ndim #获取维数"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T06:48:32.362632300Z",
     "start_time": "2024-07-19T06:48:32.352663100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([1, 1, 2])\n",
      "tensor([[3., 4.]])\n",
      "tensor([3., 4.])\n",
      "tensor([[3.],\n",
      "        [4.]])\n",
      "--------------------------------------------------\n",
      "tensor([[[3., 4.]]])\n"
     ]
    },
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.3.形状修改，tensor.view((3, 4)), 类似numpy中的reshape,是一种浅拷贝，仅仅形状发生改变,返回一个新的结果\n",
    "t2 = torch.Tensor([[[3,4]]])\n",
    "print(t2.size())\n",
    "print(t2.view([1,2])) #[1,2]表示1行2列\n",
    "print(t2.view([2])) # 一维tensor\n",
    "b=t2.view([2, -1]) # -1表示自动计算\n",
    "print(b)\n",
    "print('-'*50)\n",
    "print(t2) #t2的形状并没有发生改变\n",
    "# https://pytorch.org/docs/stable/tensor_view.html\n",
    "t2.storage().untyped().data_ptr() == b.storage().untyped().data_ptr() #判断两个tensor是否共享内存\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T06:43:11.387334700Z",
     "start_time": "2024-07-19T06:43:11.377447Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[100.],\n",
      "        [  4.]])\n",
      "tensor([[[100.,   4.]]])\n"
     ]
    }
   ],
   "source": [
    "b[0,0]=100\n",
    "print(b)\n",
    "print(t2) #t2的形状并没有发生改变"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T06:47:30.030655800Z",
     "start_time": "2024-07-19T06:47:30.021100700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "tensor(100.)\n",
      "tensor([[1, 3, 4],\n",
      "        [2, 4, 6]])\n",
      "tensor([[1, 2],\n",
      "        [3, 4],\n",
      "        [4, 6]])\n"
     ]
    }
   ],
   "source": [
    "#3. 获取维数\n",
    "print(t2.dim())\n",
    "\n",
    "#4.获取最大值\n",
    "print(t2.max())\n",
    "\n",
    "#5.转置\n",
    "t3 = torch.tensor([[1,3,4], [2,4,6]])\n",
    "print(t3)\n",
    "print(t3.t()) #转置\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T06:48:52.681098900Z",
     "start_time": "2024-07-19T06:48:52.655593100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 3, 4])\n",
      "--------------------------------------------------\n",
      "torch.Size([3, 2, 4])\n",
      "--------------------------------------------------\n",
      "torch.Size([3, 2, 4])\n",
      "--------------------------------------------------\n",
      "torch.Size([3, 4, 2])\n",
      "--------------------------------------------------\n",
      "torch.Size([4, 3, 2])\n"
     ]
    }
   ],
   "source": [
    "# 交换轴\n",
    "t4 = torch.tensor(np.arange(24).reshape(2,3,4))\n",
    "print(t4.shape)\n",
    "print(\"-\"*50)\n",
    "print(t4.transpose(0,1).shape)#交换0轴和1轴\n",
    "print(\"-\"*50)\n",
    "print(t4.permute(1, 0, 2).shape)#交换0轴和1轴,功能同上\n",
    "print(\"-\"*50)\n",
    "print(t4.permute(1, 2, 0).shape)#变为了3*4*2\n",
    "print(\"-\"*50)\n",
    "print(t4.permute(2, 1, 0).shape)#变为了4*3*2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T06:50:04.408107700Z",
     "start_time": "2024-07-19T06:50:04.389904100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.int32"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t4.dtype"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T06:51:56.607639200Z",
     "start_time": "2024-07-19T06:51:56.594489500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T06:30:31.128553Z",
     "iopub.status.busy": "2022-07-31T06:30:31.128150Z",
     "iopub.status.idle": "2022-07-31T06:30:31.140028Z",
     "shell.execute_reply": "2022-07-31T06:30:31.138517Z",
     "shell.execute_reply.started": "2022-07-31T06:30:31.128521Z"
    },
    "ExecuteTime": {
     "end_time": "2024-07-19T06:56:04.345258700Z",
     "start_time": "2024-07-19T06:56:04.322421700Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0,  1,  2,  3],\n",
      "        [ 4,  5,  6,  7],\n",
      "        [ 8,  9, 10, 11]], dtype=torch.int8)\n",
      "tensor([[1, 1, 1, 1],\n",
      "        [1, 1, 1, 1],\n",
      "        [1, 1, 1, 1]])\n",
      "--------------------------------------------------\n",
      "tensor([[-1,  0,  1,  2],\n",
      "        [ 3,  4,  5,  6],\n",
      "        [ 7,  8,  9, 10]], dtype=torch.int8)\n"
     ]
    }
   ],
   "source": [
    "# 在方法后加_，会原地修改\n",
    "x = torch.tensor(np.arange(12).reshape(3,4),dtype = torch.int8)\n",
    "print(x)\n",
    "y= torch.ones([3,4], dtype = torch.int64)\n",
    "print(y)\n",
    "\n",
    "print('-'*50)\n",
    "x.sub_(y) # add_就地修改，不加下划线的会创建一个新的tensor来存储\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0,  1,  2,  3],\n",
      "        [ 4,  5,  6,  7],\n",
      "        [ 8,  9, 10, 11]], dtype=torch.int32)\n",
      "tensor(6, dtype=torch.int32)\n",
      "tensor([4, 5, 6, 7], dtype=torch.int32)\n",
      "tensor([1, 5, 9], dtype=torch.int32)\n",
      "tensor([[ 5,  6],\n",
      "        [ 9, 10]], dtype=torch.int32)\n",
      "tensor([[ 4,  5,  6,  7],\n",
      "        [ 8,  9, 10, 11]], dtype=torch.int32)\n"
     ]
    }
   ],
   "source": [
    "#tensor取值\n",
    "t5 = torch.tensor(np.arange(12).reshape(3,4))\n",
    "print(t5)\n",
    "print(t5[1,2]) #取值\n",
    "print(t5[1]) #取一行\n",
    "print(t5[:,1]) #取一列\n",
    "print(t5[1:3,1:3]) #取一部分\n",
    "print(t5[1:3,:]) #取一部分\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T06:56:31.847431300Z",
     "start_time": "2024-07-19T06:56:31.830775300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.],\n",
      "        [ 1.],\n",
      "        [ 2.],\n",
      "        [ 3.],\n",
      "        [ 4.],\n",
      "        [ 5.],\n",
      "        [ 6.],\n",
      "        [ 7.],\n",
      "        [ 8.],\n",
      "        [ 9.],\n",
      "        [10.],\n",
      "        [11.],\n",
      "        [12.],\n",
      "        [13.],\n",
      "        [14.],\n",
      "        [15.]])\n",
      "tensor([[16.],\n",
      "        [17.],\n",
      "        [18.],\n",
      "        [19.],\n",
      "        [20.],\n",
      "        [21.],\n",
      "        [22.],\n",
      "        [23.],\n",
      "        [24.],\n",
      "        [25.],\n",
      "        [26.],\n",
      "        [27.],\n",
      "        [28.],\n",
      "        [29.],\n",
      "        [30.],\n",
      "        [31.]])\n"
     ]
    },
    {
     "data": {
      "text/plain": "tensor(256.)"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#两个16行，1列的张量相减，求均值\n",
    "t6 = torch.tensor(np.arange(16).reshape(16,1),dtype=torch.float32)\n",
    "t7 = torch.tensor(np.arange(16,32).reshape(16,1),dtype=torch.float32)\n",
    "print(t6)\n",
    "print(t7)\n",
    "((t6-t7)**2).mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T07:05:30.121788700Z",
     "start_time": "2024-07-19T07:05:30.097803100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.]])\n",
      "tensor([[0.],\n",
      "        [1.],\n",
      "        [2.]])\n"
     ]
    },
    {
     "data": {
      "text/plain": "tensor([[0., 1., 2., 3.],\n        [3., 4., 5., 6.],\n        [6., 7., 8., 9.]])"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#初始化两个张量，一个3,4，一个3,1,运算与ndarray相同\n",
    "t8 = torch.tensor(np.arange(12).reshape(3,4),dtype=torch.float32)\n",
    "t9 = torch.tensor(np.arange(3).reshape(3,1),dtype=torch.float32)\n",
    "print(t8)\n",
    "print(t9)\n",
    "t8-t9"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T06:59:32.036841900Z",
     "start_time": "2024-07-19T06:59:32.025551Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T06:30:45.464008Z",
     "iopub.status.busy": "2022-07-31T06:30:45.463595Z",
     "iopub.status.idle": "2022-07-31T06:30:45.475402Z",
     "shell.execute_reply": "2022-07-31T06:30:45.474425Z",
     "shell.execute_reply.started": "2022-07-31T06:30:45.463971Z"
    },
    "ExecuteTime": {
     "end_time": "2024-04-25T07:01:58.452015100Z",
     "start_time": "2024-04-25T07:01:58.430021100Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    },
    {
     "data": {
      "text/plain": "tensor([[ 0,  1,  2,  3],\n        [ 4,  5,  6,  7],\n        [ 8,  9, 10, 11]], dtype=torch.int8)"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "GPU中tensor的使用：\n",
    "1.实例化device， torch.device(\"cpu\" or \"cuda:0\"),选cuda:0需要torch.cuda.is_available()==true\n",
    "2.tensor.to(device),把tensor转化为CUDA支持的tensor或者CPU支持的tensor\n",
    "\"\"\"\n",
    "print(torch.cuda.is_available())\n",
    "if torch.cuda.is_available():\n",
    "    device = torch.device(\"cuda\") #cuda device对象\n",
    "    y = torch.ones_like(x, device = device) #创建一个在cuda熵的tensor\n",
    "    x = x.to(device) #把x转为cuda上的tensor\n",
    "    z = x+y\n",
    "    print(z)\n",
    "    print(z.to(\"cpu\", torch.double))\n",
    "\n",
    "# torch.tensor([1.9806], device = \"cuda:0\")\n",
    "device = torch.device(\"cpu\")\n",
    "x.to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-19T07:15:54.756868900Z",
     "start_time": "2024-07-19T07:15:54.746404300Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[  0.,   1.,   4.,   9.],\n        [ 16.,  25.,  36.,  49.],\n        [ 64.,  81., 100., 121.]])"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 张量的运算\n",
    "t8 = torch.tensor(np.arange(12).reshape(3,4),dtype=torch.float32)\n",
    "t9 = torch.tensor(np.arange(12).reshape(3,4),dtype=torch.float32)\n",
    "t8*t9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 14.,  38.,  62.],\n        [ 38., 126., 214.],\n        [ 62., 214., 366.]])"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.mm(t8, t9.transpose(0,1)) #矩阵乘法"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-19T07:17:31.114684500Z",
     "start_time": "2024-07-19T07:17:31.107433500Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
